package com.sky.mapper;

import com.sky.entity.User;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;

@Mapper
public interface UserMapper {

    /**
     * 根据openid查询用户
     * @param openid
     * @return
     */
    @Select("select id, openid, name, phone, sex, id_number, avatar, create_time from user where openid = #{openid}")
    User getByOpenid(String openid);

    /**
     * 注册新用户
     * @param user
     */
    @Insert("insert into user (openid, name, phone, sex, id_number, avatar, create_time) " +
            "VALUES (#{openid},#{name},#{phone},#{sex},#{idNumber},#{avatar},#{createTime})")
    @Options(useGeneratedKeys = true ,keyProperty = "id")
    void insert(User user);

    /**
     * 根据id查询用户
     * @param userId
     * @return
     */
    @Select("select id, openid, name, phone, sex, id_number, avatar, create_time from user where id = #{userId}")
    User selectById(Long userId);

    /**
     * 查询用户总数 -- 在某天之前所有用户
     * @param end
     * @return
     */
    @Select("select count(*) from user  where create_time <= #{end}")
    Integer countAfter(LocalDateTime end);

    /**
     * 查询用户总数 -- 在某段时间内新增用户
     * @param begin
     * @param end
     * @return
     */
    @Select("select count(*) from user where create_time between #{begin} and #{end}")
    Integer countBetween(LocalDateTime begin, LocalDateTime end);
}
